<?php
/**
 * Goals
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class GoalsController extends BaseController {

	public function index(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$psize = I('get.psize','','intval');
		$p = I('get.p','','intval');
		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$uinfo =$MTags->index();
		$user_id = $uinfo['user_id'];
		$sector_id = $uinfo['sector_id'];
		$target_type = I('get.target_type',false,'intval');
		$goal_type = I('get.goal_type',false,'intval');
		$sector = I('get.arrSector',false,'intval');
		$t_year = (string)(I('get.t_year'));
		$where = array();
		$where['a.goal_type'] = $goal_type;
		$where['a.target_type'] = $target_type;
		if( $t_year != 'undefined'){
			$s = $t_year.'-01-01 00:00:00';
		}else {
			$s =date('Y-01-01 00:00:00', strtotime(date("Y-m-d H:i:s")));
		}
		//获取权限部门
		$MSector = M('\Admin\Model\GoalsModel:Sector');
		if($sector){
			$arrSector = $MSector->getChild($sector);
		}else{
			$arrSector = $MSector->getChild($sector_id);
		}
		//获取权限个人
		$whereUser = array();
		$whereUser['sector_id'] = array('IN',$arrSector);
		$arrUser = M('User')->where($whereUser)->select();
		$dlsit_user = array();
		$lengthUser = sizeof($arrUser);
		for($j=0;$j<$lengthUser;$j++){
			$dlsit_user[$j] = $arrUser[$j]['user_id'];
		}
		if(!$arrSector || !$arrUser){
			if(!$arrSector){
				$data_sector = array();
			}
			if(!$arrUser){
				$data_user = array();
			}
		}else{
			$arr = $MSector->getTd($arrSector,$dlsit_user);
			$nowtime = time();
			if($arr['r']){
				$res['rcode'] = 1;
				$data_sector = $arr['d'];
				$data_user = $arr['m'];
			}
		}
		$length_arrsector = sizeof($data_sector);
		$length_arruser = sizeof($data_user);
		//判断目标类型
		if($goal_type==1){
			if($target_type ==1){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arruser;$j++){
					$where['a.target'] = $data_user[$j]['user_id'];
					for($i=0;$i<12;$i++){
						$key1 = $i;
						$key2 = $i + 1;
						$where['a.start_time'] = array('EGT',date("Y-m-d H:i:s",strtotime("$s + $key1 month")));
						$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second")));
						$dlist_s[$j][$i] = 	M() ->field('a.*,b.user_id,b.nickname as object')
										->table('sales_goal as a')
										->join('user as b on b.user_id = a.target')
										->where($where)
										->order('a.goal_id desc')
										->select();
						if($dlist_s[$j][$i]){
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i][0];
						}else{
							$dlist_s[$j][$i]['target'] = $data_user[$j]['user_id'];
							$dlist_s[$j][$i]['target_type'] = $target_type;
							$dlist_s[$j][$i]['goal_type'] = $goal_type;
							$dlist_s[$j][$i]['start_time'] = date("Y-m-d H:i:s",strtotime("$s + $key1 month"));
							$dlist_s[$j][$i]['end_time'] = date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second"));
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i];
						}
					}
					$Dlist[$j]['object'] = $data_user[$j]['name'];
					$Dlist[$j]['target'] = $data_user[$j]['user_id'];
				}
			}else if($target_type ==2){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arrsector;$j++){
					$where['a.target'] = $data_sector[$j]['sector_id'];
					for($i=0;$i<12;$i++){
						$key1 = $i;
						$key2 = $i + 1;
						$where['a.start_time'] = array('EGT',date("Y-m-d H:i:s",strtotime("$s + $key1 month")));
						$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second")));
						$dlist_s[$j][$i] = 	M() ->field('a.*,b.sector_id,b.sector_name as object')
										->table('sales_goal as a')
										->join('sector as b on b.sector_id = a.target')
										->where($where)
										->order('a.goal_id desc')
										->select();
						if($dlist_s[$j][$i]){
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i][0];
						}else{
							$dlist_s[$j][$i]['target'] = $data_sector[$j]['sector_id'];
							$dlist_s[$j][$i]['target_type'] = $target_type;
							$dlist_s[$j][$i]['goal_type'] = $goal_type;
							$dlist_s[$j][$i]['start_time'] = date("Y-m-d H:i:s",strtotime("$s + $key1 month"));
							$dlist_s[$j][$i]['end_time'] = date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second"));
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i];
						}
					}
					$Dlist[$j]['object'] = $data_sector[$j]['name'];
					$Dlist[$j]['target'] = $data_sector[$j]['sector_id'];
				}
			}
		}else if($goal_type==2){
			if($target_type ==1){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arruser;$j++){
					$where['a.target'] = $data_user[$j]['user_id'];
					for($i=0;$i<4;$i++){
						$key1 = $i*3;
						$key2 = $i*3 + 3;
						$where['a.start_time'] = array('EGT',date("Y-m-d H:i:s",strtotime("$s + $key1 month")));
						$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s + $key2 month - 1 second")));
						$dlist_s[$j][$i] = 	M() ->field('a.*,b.user_id,b.nickname as object')
										->table('sales_goal as a')
										->join('user as b on b.user_id = a.target')
										->where($where)
										->order('a.goal_id desc')
										->select();
						if($dlist_s[$j][$i]){
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i][0];
						}else{
							$dlist_s[$j][$i]['target'] = $data_user[$j]['user_id'];
							$dlist_s[$j][$i]['target_type'] = $target_type;
							$dlist_s[$j][$i]['goal_type'] = $goal_type;
							$dlist_s[$j][$i]['start_time'] = date("Y-m-d H:i:s",strtotime("$s + $key1 month"));
							$dlist_s[$j][$i]['end_time'] = date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second"));
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i];
						}
					}
					$Dlist[$j]['object'] = $data_user[$j]['name'];
					$Dlist[$j]['target'] = $data_user[$j]['user_id'];
				}
			}else if($target_type ==2){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arrsector;$j++){
					$where['a.target'] = $data_sector[$j]['sector_id'];
					for($i=0;$i<4;$i++){
						$key1 = $i*3;
						$key2 = $i*3 + 3;
						$where['a.start_time'] = array('EGT',date("Y-m-d H:i:s",strtotime("$s + $key1 month")));
						$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s + $key2 month - 1 second")));
						$dlist_s[$j][$i] = 	M() ->field('a.*,b.sector_id,b.sector_name as object')
										->table('sales_goal as a')
										->join('sector as b on b.sector_id = a.target')
										->where($where)
										->order('a.goal_id desc')
										->select();
						if($dlist_s[$j][$i]){
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i][0];
						}else{
							$dlist_s[$j][$i]['target'] = $data_sector[$j]['sector_id'];
							$dlist_s[$j][$i]['target_type'] = $target_type;
							$dlist_s[$j][$i]['goal_type'] = $goal_type;
							$dlist_s[$j][$i]['start_time'] = date("Y-m-d H:i:s",strtotime("$s + $key1 month"));
							$dlist_s[$j][$i]['end_time'] = date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second"));
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i];
						}
					}
					$Dlist[$j]['object'] = $data_sector[$j]['name'];
					$Dlist[$j]['target'] = $data_sector[$j]['sector_id'];
				}
			}
		}else if($goal_type==3){
			if($target_type ==1){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arruser;$j++){
					$where['a.target'] = $data_user[$j]['user_id'];
					for($i=0;$i<2;$i++){
						$key1 = $i*6;
						$key2 = $i*6 + 6;
						$where['a.start_time'] = array('EGT',date("Y-m-d H:i:s",strtotime("$s + $key1 month")));
						$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s + $key2 month - 1 second")));
						$dlist_s[$j][$i] = 	M() ->field('a.*,b.user_id,b.nickname as object')
										->table('sales_goal as a')
										->join('user as b on b.user_id = a.target')
										->where($where)
										->order('a.goal_id desc')
										->select();
						if($dlist_s[$j][$i]){
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i][0];
						}else{
							$dlist_s[$j][$i]['target'] = $data_user[$j]['user_id'];
							$dlist_s[$j][$i]['target_type'] = $target_type;
							$dlist_s[$j][$i]['goal_type'] = $goal_type;
							$dlist_s[$j][$i]['start_time'] = date("Y-m-d H:i:s",strtotime("$s + $key1 month"));
							$dlist_s[$j][$i]['end_time'] = date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second"));
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i];
						}
					}
					$Dlist[$j]['object'] = $data_user[$j]['name'];
					$Dlist[$j]['target'] = $data_user[$j]['user_id'];
				}
			}else if($target_type ==2){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arrsector;$j++){
					$where['a.target'] = $data_sector[$j]['sector_id'];
					for($i=0;$i<2;$i++){
						$key1 = $i*6;
						$key2 = $i*6 + 6;
						$where['a.start_time'] = array('EGT',date("Y-m-d H:i:s",strtotime("$s + $key1 month")));
						$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s + $key2 month - 1 second")));
						$dlist_s[$j][$i] = 	M() ->field('a.*,b.sector_id,b.sector_name as object')
										->table('sales_goal as a')
										->join('sector as b on b.sector_id = a.target')
										->where($where)
										->order('a.goal_id desc')
										->select();
						if($dlist_s[$j][$i]){
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i][0];
						}else{
							$dlist_s[$j][$i]['target'] = $data_sector[$j]['sector_id'];
							$dlist_s[$j][$i]['target_type'] = $target_type;
							$dlist_s[$j][$i]['goal_type'] = $goal_type;
							$dlist_s[$j][$i]['start_time'] = date("Y-m-d H:i:s",strtotime("$s + $key1 month"));
							$dlist_s[$j][$i]['end_time'] = date("Y-m-d H:i:s",strtotime("$s +$key2 month -1 second"));
							$Dlist[$j]['data'][$i] = $dlist_s[$j][$i];
						}
					}
					$Dlist[$j]['object'] = $data_sector[$j]['name'];
					$Dlist[$j]['target'] = $data_sector[$j]['sector_id'];
				}
			}
		}else if($goal_type==4){
			if($target_type ==1){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arruser;$j++){
					$where['a.target'] = $data_user[$j]['user_id'];
					$where['a.start_time'] = array('EGT',$s);
					$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s + 12 month - 1 second")));
					$dlist_s[$j] = 	M() ->field('a.*,b.user_id,b.nickname as object')
									->table('sales_goal as a')
									->join('user as b on b.user_id = a.target')
									->where($where)
									->order('a.goal_id desc')
									->select();
					if(!$dlist_s[$j]){
						$dlist_s[$j][0]['target'] = $data_user[$j]['user_id'];
						$dlist_s[$j][0]['target_type'] = $target_type;
						$dlist_s[$j][0]['goal_type'] = $goal_type;
						$dlist_s[$j][0]['start_time'] = $s;
						$dlist_s[$j][0]['end_time'] = date("Y-m-d H:i:s",strtotime("$s + 12 month -1 second"));
					}
					$Dlist[$j]['data'] = $dlist_s[$j];
					$Dlist[$j]['object'] = $data_user[$j]['name'];
					$Dlist[$j]['target'] = $data_user[$j]['user_id'];
				}
			}else if($target_type ==2){
				$dlist_s = array();
				$Dlist = array();
				for($j=0;$j<$length_arrsector;$j++){
					$where['a.target'] = $data_sector[$j]['sector_id'];
					$where['a.start_time'] = array('EGT',$s);
					$where['a.end_time'] = array('ELT',date("Y-m-d H:i:s",strtotime("$s + 12 month - 1 second")));
					$dlist_s[$j] = 	M() ->field('a.*,b.sector_id,b.sector_name as object')
									->table('sales_goal as a')
									->join('sector as b on b.sector_id = a.target')
									->where($where)
									->order('a.goal_id desc')
									->select();
					if(!$dlist_s[$j]){
						$dlist_s[$j][0]['target'] = $data_sector[$j]['sector_id'];
						$dlist_s[$j][0]['target_type'] = $target_type;
						$dlist_s[$j][0]['goal_type'] = $goal_type;
						$dlist_s[$j][0]['start_time'] = $s;
						$dlist_s[$j][0]['end_time'] = date("Y-m-d H:i:s",strtotime("$s + 12 month -1 second"));
					}
					$Dlist[$j]['data'] = $dlist_s[$j];
					$Dlist[$j]['object'] = $data_sector[$j]['name'];
					$Dlist[$j]['target'] = $data_sector[$j]['sector_id'];
				}
			}
		}
		$cnt = sizeof($Dlist);
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$dlist = array();
		for($i=($p-1)*$psize;$i<$p*$psize;$i++){
			if($Dlist[$i]){
				$dlist[$i] = $Dlist[$i];
			}
		}
		if($dlist){
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['ssss'] = $s;
			$res['pall'] = $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}
		e_json($res);
	}
    /**
     * 编辑营销信息
     */
    public function editGoal(){
    	$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
    	$goal_id = I('post.goal_id',false,'intval');
    	$goal = I('post.edit_goal',false,'trim');

    	$d = array();
    	$d['goal'] = $goal;

		$wheres = array();
		$wheres['goal_id'] = $goal_id;
    	$MGoal = M('goal','sales_');
		if($MGoal->where($wheres)->save($d)){
			$res['rcode'] = 1;
			$res['data'] = $MGoal->where($wheres)->select();
 		}
 		e_json($res);
    }
    /**
     * 添加营销信息
     */
    public function addGoal(){
    	$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
    	$target = I('post.target',false,'intval');
    	$target_type = I('post.target_type',false,'intval');
    	$start_time = I('post.start_time',false,'trim');
    	$end_time = I('post.end_time',false,'trim');
    	$goal = I('post.add_goal',false,'trim');
    	$goal_type = I('post.goal_type',false,'trim');

		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$uinfo =$MTags->index();

    	$d = array();
    	$d['target'] = $target;
    	$d['target_type'] = $target_type;
    	$d['start_time'] = $start_time;
    	$d['end_time'] = $end_time;
    	$d['goal'] = $goal;
    	$d['goal_type'] = $goal_type;
    	$d['creator'] = $uinfo['user_id'];
    	$d['creator_sector'] = $uinfo['sector_id'];
    	$d['creator_role'] = $uinfo['role_id'];
    	$d['create_time'] = date('Y-m-d H:i:s');

    	$MGoal = M('goal','sales_');
    	$addID = $MGoal->add($d);
    	if($addID){
    		$res['rcode'] = 1;
    	}
    	e_json($res);
    }
	/**
	 * 获取目标对象信息
	 */
	public function getObject(){
		$res = array('rcode'=>0,'sector'=>NULL,'user'=>NULL,'msg'=>'OK');
		$sector_id = I('get.sector_id',false,'intval');

		$MSector = M('\Admin\Model\GoalsModel:Sector');
		$arr = $MSector->getChild($sector_id);
		if($arr){
			$MSector = M('Sector');
			$MUser = M('User');
			$wheres = array();
			$wheres['sector_id'] = array('IN',$arr);
			$wheres['_string'] = '1=1';
			$sector = $MSector->field('sector_id as id,sector_name as name')->where($wheres)->select();
			$where = array();
			$where['sector_id'] = array('IN',$arr);
			$where['_string'] = '1=1';
			$user = $MUser->field('user_id as id,nickname as name,sector_id')->where($where)->select();

			if($sector || $user){
				$res['rcode'] = 1;
				$res['sector'] = $sector;
				$res['user'] = $user; 
				$length_sector = sizeof($sector);
				$length_user = sizeof($user);
				$length = $length_sector + $length_user;
				for($i = 0;$i<$length_user;$i++){
					$sector[$i+$length_sector] = $user[$i]; 
				}
				$res['sectorUser'] = $sector;
			}
		}
		e_json($res);
	}

}